{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Gaussian elimination"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[  2.,  -2.,  12.,  -1.],\n",
              "       [  1.,   8.,  -5.,  -3.],\n",
              "       [  1.,  -2.,  -6.,  -1.],\n",
              "       [ -2.,   3.,  -8.,  -4.]])"
            ]
          },
          "execution_count": 18,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "\n",
        "np.random.seed(5)\n",
        "n = 4\n",
        "A = np.round(np.random.randn(n, n) * 5)\n",
        "A"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now compute `A1` to eliminate `A[1,0]`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[  2. ,  -2. ,  12. ,  -1. ],\n",
              "       [  0. ,   9. , -11. ,  -2.5],\n",
              "       [  1. ,  -2. ,  -6. ,  -1. ],\n",
              "       [ -2. ,   3. ,  -8. ,  -4. ]])"
            ]
          },
          "execution_count": 23,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "A1 = A.copy()\n",
        "A1[1] -= 1/2*A1[0]\n",
        "A1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And `A2` with `A[2,0] == 0`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[  2. ,  -2. ,  12. ,  -1. ],\n",
              "       [  0. ,   9. , -11. ,  -2.5],\n",
              "       [  0. ,  -1. , -12. ,  -0.5],\n",
              "       [ -2. ,   3. ,  -8. ,  -4. ]])"
            ]
          },
          "execution_count": 25,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "A2 = A1.copy()\n",
        "A2[2] -= 1/2*A[0]\n",
        "A2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {},
  "nbformat": 4,
  "nbformat_minor": 0
}